<?php

namespace Fund\Points;

use app\api\model\User;
use app\api\model\UserPoints;
use app\api\model\UserPointsLog;
use think\facade\Db;

/**
 * 增加积分
 * Class Add
 * @package App\Libraries\Fund\Commission
 */
class Add extends Fund
{
    protected $remark = '';

    protected $direct = UserPointsLog::DIRECT_ADD;

    public function __construct($user, $money, $type, $params = [])
    {
        $this->user = $user;
        $this->money = $money;
        $this->params = $params;
        $this->business_type = $type;
        $this->targetId = $params['target_id'] ?? 0;
    }

    /**
     * @throws \Exception
     */
    public function action()
    {
        $this->modify();
    }

    /**
     * 修改金额
     * @param UserPoints $userPoints
     * @param String $direct
     * @return array
     */
    public function _change(User $user, string $direct): array
    {
        // TODO: Implement _change() method.
        $data = [
            'before' => [
                'balance'  => $user->balance ?? 0,
//                'version' => $user->version ?? 1
            ]
        ];
        $user->balance = Db::raw("balance {$direct} {$this->money}");
//        $userPoints->total_points = Db::raw("total_points + {$this->money}");
//        $userPoints->version = Db::raw("version + 1");
        $user->save();

        $user->refresh();
        $data['after'] = [
            'balance'  => $user->balance,
//            'version' => $user->version
        ];
        return $data;
    }

    /**
     * 设置业务类型
     */
    public function setBusinessType()
    {
        // TODO: Implement setBusinessType() method.
    }
}
